<template>
  <div class="permission-container">
    <div class="app-container">
      <el-card>
        <el-row type="flex" justify="end">
          <el-col :span="3">
            <el-button type="primary" icon="el-icon-edit" @click="addPermission(1,'0')">添加菜单</el-button>
          </el-col>
        </el-row>
        <el-table
          :data="list"
          row-key="id"
          style="width: 100%;margin-top:10px"
          border
        >
          <el-table-column
            label="菜单名称"
            prop="name"
          />
          <el-table-column
            label="权限标识"
            prop="code"
          /><el-table-column
            label="描述"
            prop="description"
          /><el-table-column
            label="操作"
          >
            <template slot-scope="{ row }">
              <el-button v-if="row.type===1" type="text" @click="addPermission(2,row.id)">添加</el-button>
              <el-button type="text" @click="editPermission(row.id)">编辑</el-button>
              <el-button type="text" @click="delPermission(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 新增编辑权限的弹层 -->
      <el-dialog :visible="showDialog" :title="showText" @close="btnCancel">
        <el-form ref="addPermissionRef" :model="formData" :rules="rules" label-width="100px">
          <el-form-item prop="name" label="权限名称">
            <el-input v-model="formData.name" placeholder="" />
          </el-form-item>
          <el-form-item prop="code" label="权限标识">
            <el-input v-model="formData.code" placeholder="" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" placeholder="" />
          </el-form-item>
          <el-form-item label="企业可见">
            <el-switch
              v-model="formData.enVisible"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button type="primary" @click="btnOK">确认</el-button>
            <el-button @click="btnCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermissionList, addPermission, updatePermission, delPermission, getPermissionDetail } from '@/api/permission'
import { transListToTreeData } from '@/utils/index'
export default {
  name: 'PermissionPage',
  data () {
    return {
      list: [],
      showDialog: false,
      formData: {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 开启
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    showText () {
      return this.formData.id ? '编辑权限' : '新增权限'
    }
  },
  created () {
    this.getPermissionList()
  },
  methods: {
    // 获取表格数据
    async getPermissionList () {
      this.list = transListToTreeData(await getPermissionList(), '0')
      // console.log(this.list)
    },
    // 删除权限
    delPermission (id) {
      this.$confirm('确认删除该权限点吗？').then(async () => {
        return await delPermission(id)
      }).then(() => {
        this.$message.success('操作成功！')
        this.getPermissionList()
      })
    },
    // 添加权限
    addPermission (type, pid) {
      this.formData.type = type
      this.formData.pid = pid
      this.showDialog = true
    },
    btnOK () {
      this.$refs.addPermissionRef.validate().then(async () => {
        if (this.formData.id) {
          return await updatePermission(this.formData)
        }
        return await addPermission(this.formData)
      }).then(() => {
        this.$message.success('操作成功！')
        this.getPermissionList()
        this.showDialog = false
      })
    },
    btnCancel () {
      this.formData = {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 开启
      }
      this.$refs.addPermissionRef.resetFields()
      this.showDialog = false
    },
    // 编辑权限
    async editPermission (id) {
      this.formData = await getPermissionDetail(id)
      this.showDialog = true
    }
  }
}
</script>

<style>
</style>
